/* CoralCube | Massively Multiplayer Online Role-Playing Game(MMORPG) Emulator.
|* 
|* Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/>
|*               2008-2010 TrinityCore <http://www.trinitycore.org/>
|*               2008-2010 CoralCube <http://www.CoralCube.com/>
|*
|* This program is free software. You can redistribute it and/or modify it under the terms of 
|* the GNU General Public License as published by the Free Software Foundation, either 
|* version 2 of the License.
|*
|* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, 
|* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
|* See the GNU General Public License for more details.
|*
|* You should have received a copy of the GNU General Public License along with this program. 
|* If not, see <http://www.gnu.org/licenses/>.
|*/

/** \file
    \ingroup Trinityd
 */

#include "Common.h"
#include "Config.h"
#include "Log.h"
#include "RARunnable.h"
#include "World.h"

#include <ace/Reactor_Impl.h>
#include <ace/TP_Reactor.h>
#include <ace/Dev_Poll_Reactor.h>
#include <ace/Acceptor.h>
#include <ace/SOCK_Acceptor.h>

#include "RASocket.h"

RARunnable::RARunnable() : m_Reactor(NULL)
{
	ACE_Reactor_Impl* imp = 0;

#if defined (ACE_HAS_EVENT_POLL) || defined (ACE_HAS_DEV_POLL)

	imp = new ACE_Dev_Poll_Reactor();

	imp->max_notify_iterations (128);
	imp->restart (1);

#else

	imp = new ACE_TP_Reactor();
	imp->max_notify_iterations (128);

#endif

	m_Reactor = new ACE_Reactor (imp, 1);
}

RARunnable::~RARunnable()
{
	delete m_Reactor;
}

void RARunnable::run()
{
	if (!sWorld.getBoolConfig(CONFIG_RA_ENABLE))
		return;

	ACE_Acceptor<RASocket, ACE_SOCK_ACCEPTOR> acceptor;

	uint16 raport = sWorld.getIntConfig(CONFIG_RA_PORT);
	std::string stringip = sConfig.GetStringDefault("Ra.IP", "0.0.0.0");

	ACE_INET_Addr listen_addr(raport, stringip.c_str());

	if (acceptor.open(listen_addr, m_Reactor) == -1)
	{
		sLog.outError("RA can not bind to port %d on %s", raport, stringip.c_str());
		return;
	}

	sLog.outString("Starting RA on port %d on %s", raport, stringip.c_str());

	while (!World::IsStopped())
	{
		// don't be too smart to move this outside the loop
		// the run_reactor_event_loop will modify interval
		ACE_Time_Value interval(0, 100000);

		if (m_Reactor->run_reactor_event_loop(interval) == -1)
			break;
	}

	sLog.outStaticDebug("RA thread exiting");
}